#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
#include <cassert>
#include <vector>
#include <algorithm>
#include <cstdbool>
namespace lnb
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;
		vector()
			:_start(nullptr),
			_finish(nullptr),
			_endofstoage(nullptr)
		{}

		template <class InputIterator>        
		vector(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				push_back(*first);
				first++;
			}
		}

		~vector()
		{
			if (_start != nullptr)
			{
				delete[] _start;
			}
			_start = _finish = _endofstoage = nullptr;
		}

		vector(int n, const T& t = T())
		{
			reserve(n);
			for (int i = 0; i <n; i++)
			{
				_start[i] = t;
			}
			_finish = _start + n;
		}

		void reserve(size_t csize)
		{
			if (csize <= capacity())
			{
				return;
			}
			size_t befsize =size();
			iterator tmp;
			tmp = new T[csize];
			if (_start)
			{
				for (int i = 0; i < size(); i++)
				{
					tmp[i] = _start[i];
				}
				delete[] _start;
			}
			_start = tmp;
			_finish = _start +befsize;
			_endofstoage = _start+csize;
		}

		vector(const vector& v)
		{
			_start = new T[v.capacity()];
			_endofstoage = _start + v.capacity();
			_finish = _start + v.size();
			for (int i = 0; i < size(); i++)
			{
				_start[i] = v[i];
			}
		}

		vector<T>& operator=(const vector<T>& v)
		{
			delete[] _start;
			_start = new T[v.capacity()];
			_endofstoage = _start + v.capacity();
			_finish = _start;
			for (int i = 0; i < v.size(); i++)
			{
				*_finish = v[i];
				_finish++;
			}
			return *this;
		}

		T& operator[](size_t i)
		{
			return _start[i];
		}

		const T& operator[](size_t i)const
		{
			return _start[i];
		}

		size_t size()const
		{
			return _finish - _start;
		}


		size_t capacity()const
		{
			return _endofstoage - _start;
		}

		bool empty()const
		{
			return _start == _finish;
		}

		void push_back(const T& x)
		{
			if (_finish == _endofstoage)
			{
				reserve(capacity() == 0 ? 4 : capacity() * 2);
			}
			*_finish = x;
			_finish++;
		}

		void pop_back()
		{
			assert(!empty());
			_finish--;
		}

		void resize(size_t n,const T& x=T())
		{
			if (n <= size())
			{
				_finish = _start + n;
			}
			else
			{
				if (n > capacity())
				{
					reserve(n);
				}
				size_t befsize = size();
				for (int i = befsize; i < n; i++)
				{
					*_finish = x;
					_finish++;
				}
			}
		}

		iterator begin()
		{
			return _start;
		}

		iterator begin()const 
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator end()const
		{
			return _finish;
		}

		iterator insert(iterator pos, const T& x)
		{
			assert(pos>=_start);
			assert(pos<= _finish);
			size_t size = pos - _start;
			if (_finish == _endofstoage)
			{
				reserve(capacity() == 0 ? 4 : capacity() * 2);
			}
			pos=_start+size;
			iterator end = _finish;
			while (end > pos)
			{
				*end =*(end - 1);
				end--;
			}
			*pos = x;
			_finish++;
			return pos;
		}

		iterator erase(iterator pos)
		{
			assert(pos < _finish);
			assert(pos >= _start);
			iterator begin = pos+1;
			while (begin <_finish)
			{
				*(begin - 1) = *begin;
				begin++;
			}
			_finish--;
			return pos;
		}

	private:
		iterator _start;
		iterator _finish;
		iterator _endofstoage;
	};


	void Test()
	{
		vector<lnb::string> v;
		v.push_back("1");
		cout << v.size() << endl;
		cout << v.capacity() << endl;
		vector<lnb::string> vv(v);
		cout << vv.size() << endl;
		cout << vv.capacity() << endl;
		cout << vv[0] << endl;
		cout << vv.empty() << endl;
		vv.push_back("2");
		v = vv;
		cout << "----------------------------------" << endl;
		cout << v[1] << endl;
		v.pop_back();
		for (int i = 0; i < v.size(); i++)
		{
			cout << v[i] << endl;
		}
		v.reserve(10);
		cout << v.capacity() << endl;
		cout << "----------------------------------" << endl;
		v.push_back("2");
		v.push_back("3");
		v.push_back("4");
		v.push_back("5");
		vector<lnb::string> vvv(vv.begin(), vv.end());
		for (int i = 0; i < v.size(); i++)
		{
			cout << v[i] << endl;
		}
		cout << "----------------------------------" << endl;
		v.resize(10,"heihei");
		for (int i = 0; i < v.size(); i++)
		{
			cout << v[i] << endl;
		}
		cout << "----------------------------------" << endl;

		vector<int> v1(3, 1);
		for (int i = 0; i < v1.size(); i++)
		{
			cout << v1[i] << endl;
			
		}
		cout << v1.size() << endl;
		cout << v1.capacity();
	}


	void Test2()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(5);
		v.push_back(6);
		auto pos=find(v.begin(), v.end(), 2);
		v.erase(pos);
		for (int i = 0; i < v.size(); i++)
		{
			cout << v[i] << endl;

		}
		cout << v.size() << endl;
		cout << v.capacity();
	}



}